(function(){
    App.modules.MinionList = function(){
        var breadcrumb = new App.models.Breadcrumb({
            prefix: [
                {text: '主机管理', anchor: '/task'}
            ],
            curr: '节点列表'
        });

        var AcceptedTableOpts = {
            el: '#accepted .bootstrap-table',
            columns: [
                {field: 'minionId',     title: '节点ID'},
                {
                    field: 'hwareSN',
                    title: '硬件序列号',
                    formatter: function(value, row, index){
                        return widthRestrictedPopover(value, value, 'wordcut-240');
                    }
                },
                {
                    field: 'IP',
                    title: 'IP',
                    formatter: function(value, row, index){
                        if(!value || value == '') return '';

                        var _list = [];
                        $.map(value.split(','), function(val, idx){
                            if(val != '127.0.0.1') _list.push(val);
                        });
                        if(_list.length == 1) return _list;
                        var first = _list[0];
                        var popText = _list.join('<br/>');
                        return collapsedPopover(popText, first);
                    }
                },
                {field: 'status',     title: '状态'},
                {
                    field: 'operation',
                    title: '操作',
                    class: 'operate-col',
                    formatter: function (value, row, index) {
                        if(row.status){
                            var btnClass = 'btn-default';
                        }else{
                            var btnClass = 'btn-danger';
                        }
                        return minionOperationGroup(btnClass, row.minionId);                
                    }
                }
            ],
            opts: {
                url: App.apiList.minionList + '/query/accepted',
                toolbar: '#accepted .toolbar',
            },
        };
        var AcceptedTable = App.views.BaseTableView.extend({        
            listener: function(){
                this.$el.on('click', '.btn-exec-inline', this.execInline.bind(this));
                this.$el.on('click', '.btn-detail-inline', this.detailInline.bind(this));
            },
            execInline: function(){},
            detailInline: function(){},
        });

        var PreTableOpts = {
            el: '#pre .bootstrap-table',
            columns: [
                {field: 'minionId',     title: '节点ID'},
                {
                    field: 'operation',
                    title: '操作',
                    class: 'operate-col',
                    formatter: function (value, row, index) {
                        if(row.status){
                            var btnClass = 'btn-default';
                        }else{
                            var btnClass = 'btn-danger';
                        }
                        return minionOperationGroup(btnClass, row.minionId);                
                    }
                }
            ],
            opts: {
                url: App.apiList.minionList + '/query/pre',                
                toolbar: '#pre .toolbar',
            },
        }
        var PreTable = App.views.BaseTableView.extend({        
            listener: function(){
            },
        });

        var RejectedTableOpts = {
            el: '#rejected .bootstrap-table',
            columns: [
                {field: 'minionId',     title: '节点ID'},
                {
                    field: 'operation',
                    title: '操作',
                    class: 'operate-col',
                    formatter: function (value, row, index) {
                        if(row.status){
                            var btnClass = 'btn-default';
                        }else{
                            var btnClass = 'btn-danger';
                        }
                        return minionOperationGroup(btnClass, row.minionId);                
                    }
                }
            ],
            opts: {
                url: App.apiList.minionList + '/query/rejected',                
                toolbar: '#rejected .toolbar',
            },
        }
        var RejectedTable = App.views.BaseTableView.extend({        
            listener: function(){
            },
        });

        var DeniedTableOpts = {
            el: '#denied .bootstrap-table',
            columns: [
                {field: 'minionId',     title: '节点ID'},
                {
                    field: 'operation',
                    title: '操作',
                    class: 'operate-col',
                    formatter: function (value, row, index) {
                        if(row.status){
                            var btnClass = 'btn-default';
                        }else{
                            var btnClass = 'btn-danger';
                        }
                        return minionOperationGroup(btnClass, row.minionId);                
                    }
                }
            ],
            opts: {
                url: App.apiList.minionList + '/query/denied',                
                toolbar: '#denied .toolbar',
            },
        }
        var DeniedTable = App.views.BaseTableView.extend({        
            listener: function(){
            },
        });

        var MainView = Backbone.View.extend({
            // el与initialize一样是bk的关键字, 不能重命名为其它
            el: '#page-cnt',
            template:  _.template($("#tpl-minion-list").html()),
            events: {},
            initialize: function() {
                var _self = this;
                // 创建面包屑视图
                _self.breadcrumbView = new App.views.BreadcrumbView({model: breadcrumb});
                _self.$el.html(_self.template());

                _self.acceptedModel = new App.models.Minion();
                _self.preModel = new App.models.Minion();
                _self.deniedModel = new App.models.Minion();
                _self.rejectedModel = new App.models.Minion();

                _self.acceptedTable = new AcceptedTable(Object.assign(
                    {
                        eventBus: _self.eventBus,
                        dataModel: _self.acceptedModel
                    },
                    AcceptedTableOpts, 
                ));
                _self.preTable = new PreTable(Object.assign(
                    {
                        eventBus: _self.eventBus,
                        dataModel: _self.preModel                        
                    },
                    PreTableOpts, 
                ));
                _self.rejectedTable = new RejectedTable(Object.assign(
                    {
                        eventBus: _self.eventBus,
                        dataModel: _self.rejectedModel                        
                    },
                    RejectedTableOpts, 
                ));
                _self.deniedTable = new RejectedTable(Object.assign(
                    {
                        eventBus: _self.eventBus,
                        dataModel: _self.deniedModel                        
                    },
                    DeniedTableOpts, 
                ));

                _self.watch();
            },
            watch: function(){
                var _self = this;
                this.listenTo(_self.acceptedModel, 'requestSuccess', function(){
                    _self.acceptedTable.$el.bootstrapTable('refresh');
                });
                this.listenTo(_self.preModel, 'requestSuccess', function(){
                    _self.preTable.$el.bootstrapTable('refresh');
                });
                this.listenTo(_self.deniedModel, 'requestSuccess', function(){
                    _self.deniedTable.$el.bootstrapTable('refresh');
                });
                this.listenTo(_self.rejectedModel, 'requestSuccess', function(){
                    _self.rejectedTable.$el.bootstrapTable('refresh');
                });
            },
        });
        var mainView = new MainView();
    }
})();
